Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

163
Vistas
React input[type="range"] with custom tick labels

I have the following React component:

  // Datalist for custom tick mark display
  const datalistId = `slider-datalist-${uniqueId()}`;
  const datalistItems = [];
  for (let i=min; i<=max; i += stepSize) {
    if (labelRenderer) {
      datalistItems.push(<option key={i} value={i} label={labelRenderer(i)} />)
    } else {
      datalistItems.push(<option key={i} value={i} label={i} />)
    }
  }

  return (
    <div>
      <input
        type="range"
        id={id}
        value={value}
        min={min}
        max={max}
        step={stepSize}
        onChange={handleChange}
        list={datalistId}
      />
      <datalist id={datalistId}>
        {datalistItems}
      </datalist>
    </div>
  );

Things work fine with labelRenderer when a basic value (i.e. string or number) is provided:

    <Slider
      min={1} 
      max={10} 
      stepSize={1}
      labelRenderer={v => { 
        return `**${v}**`;
      }} 
    />

working tick labels

But things blow up when I try to get it to render React JSX:

    <Slider
      min={1} 
      max={10} 
      stepSize={1}
      labelRenderer={v => { 
        return (<span style={{ color: '#f00;' }}>{v}</span>);
      }} 
    />

bad tick labels

Does anyone know why? I would have expected React to handle the output here just fine.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

The label attribute casts everything as a string. So you have to find another way to render the different elements, if you just need to change the style, I think with the right names (like using data-color="#f00", for example) you can do it just from CSS.

about 4 years ago · Juan Pablo Isaza Denunciar

0

You are trying to pass a JSX object to an attribute of an HTML tag (option). Here, I've used input instead of option tag to clarify what exactly happens. Even if you pass JSX like this <option>{JSX here!}</option> I won't work because <option> doesn't support any nested tag.

   const ChildComponent = function({callBack}) {
      
      let aNumber = 10; 
      let theList = [<input value={callBack(aNumber)}/>,
      <div>{callBack(aNumber)} fixed</div>];
    
      return (<div>{theList}</div>);
    }
    
    
    export default function App() {
      return (
        <div className="App">
          <ChildComponent  callBack={num => {
            //return `${num}`;  // Converted to String
            return (<span>{num}</span>); // Return Object
          }} />
        </div>
      );
    }

https://codesandbox.io/s/blissful-jackson-k6bjt1?file=/src/App.js:24-469

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda